import { Component, OnInit } from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {PaygoodsService} from "../../../services/paygoods.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-shopcar',
  templateUrl: './shopcar.component.html',
  styleUrls: ['./shopcar.component.scss']
})
export class ShopcarComponent implements OnInit {
  // 显示全图灰色透明背景
  public color: any = false;
  // 是否显示删除框
  public showConfirmDel: any = false;
 // 获取购物车商品信息，用于筛选信息
  public goods: any = [];
  // 获取所有商品信息
  public goodsmsg: any = [];
  // 显示所有的绿色小圆点
  public showAllGreen: any = 0;
  // 显示绿色小圆点
  public showGreen: any = [];
  // 选中商品总件数
  public num: any = 0;
  // 选中商品的价钱
  public payMoney: any = 0;
  // 存放选中商品id
  public goodsId: any = [];
  // 隐藏列表
  public hiddenList: any = [];
  // 控制全选按钮，显示隐藏
  public hiddenCheckAll: any = true;
  // post请求参数
  public params: any = {};
  public k: any = 0;
  constructor( private http: HttpClient, private service: PaygoodsService, private router: Router) { }

  ngOnInit() {
    const url = 'http://192.168.2.228:8080/shoppingcart/personal?user_id=' + localStorage.user;
    this.http.get(url).subscribe( res => {
      this.goodsmsg = res['data'];
      if ( this.goodsmsg.length === 0) {
        this.hiddenCheckAll = false;
        return;
      }
      for ( let i = 0; i < res['data'].length; i++) {
        this.showGreen[i] = 0;
        // 给隐藏数组中加true
        this.hiddenList[i] = true;
      }
    });
  }
  // 判断数组里面选中信息的总数量（选中时）
  showAllcount() {
    this.num = 0;
    for( let i = 0; i < this.showGreen.length; i++) {
      if ( this.showGreen[i] === 1) {
        this.num = this.num + 1;
      }
    }
    return this.num;
  }
  // 计算选中商品价格
  payGoodsMoney( arr) {
    this.payMoney = 0;
    for ( let i = 0; i < arr.length; i++) {
      if ( arr[i] === 1) {
        this.payMoney = this.payMoney + ( this.goodsmsg[i]['g_price'] - this.goodsmsg[i].g_discount ) * this.goodsmsg[i].goods_count;
      }
    }
    return this.payMoney;
  }
  // 点击选择所有按钮时
  showAllgreen() {
    if ( this.showAllGreen === 0) {
      // 选中所有
       this.showAllGreen = 1;
       for ( let i = 0; i < this.showGreen.length; i ++) {
         this.showGreen[i] = 1;
       }
       this.num = this.showGreen.length;
       this.payGoodsMoney( this.showGreen);
    } else {
      this.showAllGreen = 0;
      //   清空钱数
      this.payMoney = 0;
      this.num = 0;
      for ( let i = 0; i < this.showGreen.length; i ++) {
        this.showGreen[i] = 0;
      }
    }
  }
  checkMsg(k) {
    if ( this.showGreen[k] === 0) {
      // 选中状态
      this.showGreen[k] = 1;
      this.showAllcount();
      this.payGoodsMoney( this.showGreen);
    } else {
      // 未选中状态
      this.showGreen[k] = 0;
      this.num = this.num - 1;
      this.showAllcount();
      this.payGoodsMoney( this.showGreen);
    }
  }
  delGoods(k) {
    // 判断是否选中元素
    this.k = k;
    if ( this.showGreen[k] === 1) {
      this.color = true;
      this.showConfirmDel = true;
      this.goodsId.push(this.goodsmsg[k].g_id);
      const param = {
        user_id: localStorage.user,
        goods_ids: this.goodsId
      };
      this.params = param;
    }
  }
  delAllGoods() {
    for ( let i = 0; i < this.showGreen.length; i ++) {
      if ( this.showGreen[i] === 1) {
        // 说明有选中元素
        this.color = true;
        this.showConfirmDel = true;
        console.log("选中商品" + this.showGreen);
        // 把选中商品遍历
        console.log("+++++" + this.goodsId);
        this.goodsId.push(this.goodsmsg[i].g_id);
        console.log(this.goodsId);
        const param = {
          user_id: localStorage.user,
          goods_ids: this.goodsId
        };
        this.params = param;
      }
    }
  }
  cancelDel() {
    this.showConfirmDel = false;
    this.color = false;
  }
  confirmDelete() {
    const url = 'http://192.168.2.228:8080/shoppingcart/delete';
    const heads = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    };
    this.http.post( url, this.params, heads).subscribe( res => {
      this.showConfirmDel = false;
      this.color = false;
      console.log(this.params.goods_ids);
      if ( this.params.goods_ids.length === this.goodsmsg.length) {
        // 说明删除了所有
        // 把选中列表清空
        this.goodsId = [];
        this.payMoney = 0;
        this.num = 0;
        // 释放所有选中小原点
        this.showAllGreen = false;
        for (let i = 0; i < this.goodsmsg.length; i++) {
          if (this.showGreen[i] === 1) {
            // 然后把选中商品隐藏
            this.hiddenList[i] = false;
            this.showGreen[i] = 0;
          }
        }
        // 判断商品列表是否为空
        for ( let i = 0; i < this.hiddenList.length; i ++) {
          if (this.hiddenList[i]) {
            this.hiddenCheckAll = true;
            return;
          } else {
            this.hiddenCheckAll = false;
          }
        }
      } else {
        // 把删除的商品从页面隐藏
        this.hiddenList[this.k] = false;
        // 把选中元素释放
        this.showGreen[this.k] = 0;
        console.log("+++++" + this.showGreen);
        // 重新计算价钱
        this.payGoodsMoney(this.showGreen);
        // 重新计算数量
        this.showAllcount();
        // 判断信息是否都隐藏了，如果是则关闭全选按钮
        for (let i = 0; i < this.hiddenList.length; i++) {
          if (this.hiddenList[i]) {
            this.hiddenCheckAll = true;
            return;
          } else {
            this.hiddenCheckAll = false;
          }
        }
      }
    });
    }
    // 点击图片进入商品详情
  goDetail(k) {
    this.router.navigate(['/goodsdetails', this.goodsmsg[k].g_id]);
  }
    paymoney() {
      this.goodsId = [];
      for ( let i = 0 ; i < this.showGreen.length; i ++) {
        if ( this.showGreen[i] === 1) {
          this.goodsId.push(this.goodsmsg[i].g_id);
          console.log(this.goodsId);
        }
      }
      console.log("鸟蛤" + this.goodsId);
      this.service.setGoodsmsg(this.goodsId);
    }
}
